package com.test.latitudex.utilities;

import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.test.latitudex.FriendsFragment;
import com.test.latitudex.InvitationsFragment;
import com.test.latitudex.R;

public class InvitationsListAdapter extends BaseAdapter {
	private Activity activity;
	private static LayoutInflater inflater = null;
	public static final String KEY_UNSEEN = "unseen";
	public static final String KEY_OUTGOING = "outgoing";
	public static final String KEY_ID = "id";
	public static final String KEY_GUID = "guid";
	public static final String KEY_EMAIL = "email";
	public static final String KEY_DISPLAYNAME = "displayName";
	public static final String KEY_AVATAR = "avatar";
	@SuppressWarnings("unused")
	private static final String LOG_TAG = "InvitationActivity";
	
	public InvitationsListAdapter(Activity a) {
		activity = a;
		inflater = (LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
	}

	public int getCount() {
		return RegistrationManager.getEntity().getInvitationData().size();
	}

	public Object getItem(int position) {
		return position;
	}

	public long getItemId(int position) {
		return position;
	}

	
	
	
	public View getView(final int position, View convertView, ViewGroup parent) {
		View vi = convertView;
		if (convertView == null)
			vi = inflater.inflate(R.layout.invitation_list_row, null);
		ImageView thumb_image = (ImageView) vi.findViewById(R.id.list_image);
		ImageView accept = (ImageView) vi.findViewById(R.id.accept_invitation);
		ImageView discard = (ImageView) vi.findViewById(R.id.discard_invitation);
		
		accept.setClickable(true);
		discard.setClickable(true);
		
		accept.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				synchronized (this) {
					new Thread(new Runnable() {

						@Override
						public void run() {
							try {
								RegistrationManager.getEntity()
										.acceptInvitation(
												RegistrationManager.getEntity().getInvitationData().get(position)
														.get(KEY_GUID));
								InvitationsFragment.refreshInviteList();
								FriendsFragment.refreshFriendData();
							} catch (Exception e) {
								activity.runOnUiThread(new Runnable() {

									@Override
									public void run() {
										Toast.makeText(activity,
												"Network communication unsuccesful. Please check network connection!",
												Toast.LENGTH_LONG).show();
									}

								});
							}
							
						}

					}).start();

				}

			}
			
		});
		
		discard.setOnClickListener(new OnClickListener(){

			@Override
			public void onClick(View v) {
				synchronized(this){
					new Thread(new Runnable(){

						@Override
						public void run() {
							try {
								RegistrationManager.getEntity().deleteInvitation(RegistrationManager.getEntity().getInvitationData().get(position).get(KEY_GUID));
								InvitationsFragment.refreshInviteList();
							} catch (Exception e) {
								activity.runOnUiThread(new Runnable(){

									@Override
									public void run() {
										Toast.makeText(activity, "Network communication unsuccesful. Please check network connection!",
												Toast.LENGTH_LONG).show();
									}
									
								});
							}
							
						}
						
					}).run();		
				}
					
			}
			
		});
		
		TextView name = (TextView) vi.findViewById(R.id.name);
		name.setText(RegistrationManager.getEntity().getInvitationData().get(position).get(KEY_DISPLAYNAME));
		TextView email = (TextView) vi.findViewById(R.id.email);
		email.setText(RegistrationManager.getEntity().getInvitationData().get(position).get(KEY_EMAIL));
		
		if(RegistrationManager.getEntity().getInvitationData().get(position).get(KEY_OUTGOING).equals("true")){
			vi.findViewById(R.id.accept_invitation).setVisibility(View.GONE);
		}
		
		Drawable d = activity.getResources().getDrawable(R.drawable.thumb);
		thumb_image.setImageDrawable(d);
		return vi;
	}
}